<template>
    <el-dialog
        title="返佣比例详情"
        center
        :visible.sync="visible"
        width="1200"
        class="audit-confirm"
        :before-close="handleCancel"
    >
    <div class="commissionsDialog">
        <AgentRebates
            ref="AgentRebates"
            :isShow="['commissionDzRebates', 'otherCommission']"
            :queryCommissionRateParams="queryCommissionRateParams"
            :agentData="agentData"
            :commissionModel="2"
            :isEdit="isEdit"
            labelWidth="170px"
            @agentPointSetAllSubOk="update"
         />
    </div>
    <div slot="footer" class="dialog-footer">
        <template v-if="isEdit">
            <el-button :loading="loading" @click="handleCancel">
                {{ $t('agent.cancel') }}
            </el-button>
            <el-button type="primary" @click="handleOK">
                {{ $t('agent.submit') }}
            </el-button>
        </template>
    </div>
    </el-dialog>
</template>
<script>
import AgentRebates from '@/components/AgentRebates'

export default {
    name: 'CommissionsDialog',
    components: {
        AgentRebates
    },
    props: {
        visible: {
            type: Boolean,
            default: false
        },
        isEdit: {
            type: Boolean,
            default: false
        },
        row: {
            type: Object,
            default: () => ({})
        }
    },
    data () {
        return {
            loading: false,
            pointAndRebate: {
                commissionModel: 2,
				commissionRateList: []
            }
        }
    },
    computed: {
        queryCommissionRateParams() {
			return {
				merchantId: this.row.merchantId,
                userName: this.row.userName,
				userId: this.row.userId,
                status: this.row.status
			}
		},
        agentData() {
			return Object.assign({}, this.row, {
				id: this.row.userId
			})
		}
    },
    methods: {
        handleCancel() {
            this.$emit('update:visible', false)
            this.$refs['AgentRebates'].resetFields()
            this.$emit('refresh')
        },

        async update({ adjustmentType }) {
            const agentRebatesRef = this.$refs['AgentRebates']
			agentRebatesRef.agentPointSetAllSubLoading = true

            try {
                const ret = await this.$api.commissionRateUpdate({
                    commissionRateList: this.$refs['AgentRebates'].getValues(['commissionDzRebates']),
                    userId: this.row.userId,
                    merchantId: this.row.merchantId,
                    status: 1,
                    adjustmentType
                })

                if (ret.code === 200) {
                    this.handleCancel()
                }
            } finally {
                agentRebatesRef.agentPointSetAllSubLoading = false
            }
           
        },
        async handleOK() {
            try {
                await this.$refs['AgentRebates'].validate()
                this.$refs['AgentRebates'].agentPointSetAllSubVisible = true
            } catch (err) {
                throw err
            }
        },
    }
}
</script>

<style scoped>
    ::v-deep .commissionsDialog .commissionModel{
         display: none;
    }
    ::v-deep .commissionsDialog .pointTable {
        display: none;
    }
</style>
